package com.metronoteapp.database;

import com.metronoteapp.model.UserModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
public class UserDao extends BaseDao {

	public static final String TABLE_NAME = "UserTbl";
	public static final String EMAIL = "email";
	public static final String PASS = "pass";
	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + 
												EMAIL + " TEXT PRIMARY KEY, " + 
												PASS + " TEXT)";
	public static final String[] COLUMNS = { EMAIL, PASS };

	public UserDao() {
	}

	public UserDao(Context _context) {
		super(_context);
	}

	// Insert new record
	public long insertUser(UserModel _user) {
		ContentValues cv = new ContentValues();
		cv.put(EMAIL, _user.getEmail());
		cv.put(PASS, _user.getPass());
		return mDB.insert(TABLE_NAME, null, cv);
	}

	// Edit one record
	public long updateUser(UserModel _user) {
		ContentValues values = new ContentValues();
		String selection = EMAIL + " = '" + _user.getEmail() + "'";
		values.put(EMAIL, _user.getEmail());
		values.put(PASS, _user.getPass());
		return mDB.update(TABLE_NAME, values, selection, null);
	}

	// Remove one record
	public boolean removeUser(UserModel _user) {
		return mDB.delete(TABLE_NAME, EMAIL + "=" + _user.getEmail(), null) > 0;
	}

	// Select all record
	public Cursor getAllRecord() {
		return mDB.query(TABLE_NAME,
				new String[] { EMAIL, PASS }, null, null, null,
				null, null);
	}

	// Select one record
	public Cursor getUserLogin(UserModel _user) {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(email) = '" + _user.getEmail().trim() + "'", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}
}
